package desensitization.controller;

import com.caigou.desensitization.annotation.AutoClearDesensitization;
import com.caigou.desensitization.strategy.DesensitizationContext;
import desensitization.entity.User;
import desensitization.mapper.UserMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class TestController {
	@Resource
	private UserMapper userMapper;

	@GetMapping("/getById")
	User get(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@AutoClearDesensitization
	@GetMapping("/findAllUser")
	List<User> findAllUser() {
		DesensitizationContext.tempDisEnabled();
		// 指定要跳过脱敏的字段
		DesensitizationContext.addFields(User.class, "mobile", "email");
		return userMapper.findAllUser();
	}

	@AutoClearDesensitization
	@GetMapping("/findAllUser2")
	List<User> findAllUser2() {

		try {
			// 指定要跳过脱敏的字段
			DesensitizationContext.addFields(User.class, "mobile", "email");
			return userMapper.findAllUser();
		}finally {
			DesensitizationContext.clear();

		}
	}
}
